﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WRJ
{
	public class BaseCommonClass
	{
		/// <summary>
		/// 数据库连接字符串
		/// </summary>
		protected static string connectionString = @"Provider=SQLOLEDB;Dsn=WRJDB;";
		private static OleDbConnection aConnection;
		/// <summary>
		/// 打开数据库连接
		/// </summary>
		public static void OpenConnection()
		{
			aConnection = new OleDbConnection(connectionString);
			try
			{
				aConnection.Open();
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);

			}
		}
		/// <summary>
		/// 关闭数据库连接
		/// </summary>
		public static void CloseConnection()
		{
			try
			{
				aConnection.Close();
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		/// <summary>
		/// 执行非查询的SQL语句
		/// </summary>
		/// <param name="command"></param>
		/// <returns></returns>
		public static int NoQueueSQL(string command)
		{
			OleDbCommand aCommand = new OleDbCommand(command);
			int ret=-1; 
			try
			{
				OpenConnection();
				ret = aCommand.ExecuteNonQuery();
				CloseConnection();
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return ret;
		}
		/// <summary>
		/// 执行查询的SQL语句
		/// </summary>
		/// <param name="command"></param>
		/// <returns></returns>
		public static OleDbDataReader QueueSQL(string command)
		{
			OleDbCommand aCommand = new OleDbCommand(command);
			OleDbDataReader aReader=null;
			try
			{
				OpenConnection();
				aReader = aCommand.ExecuteReader();
				CloseConnection();
			}
			catch (Exception e)
			{
				MessageBox.Show(e.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return aReader;
		} 
		/*
		private void LoadData(int catalog, FlowLayoutPanel flowPanel)
		{
			//创建数据库连接 
			OleDbConnection aConnection = new OleDbConnection(connectionString);

			choice = new List<ChoiceClass>();
			decide = new List<DecideClass>();

			OleDbCommand aCommand_1 = new OleDbCommand("select * from Choice where [Catalog]=" + catalog + ";", aConnection);
			OleDbCommand aCommand_2 = new OleDbCommand("select * from Decide where [Catalog]=" + catalog + ";", aConnection);
			try
			{
				aConnection.Open();
				OleDbDataReader aReader = aCommand_1.ExecuteReader();

				Label item1 = new Label();
				item1.Text = "选择题";
				item1.AutoSize = true;
				item1.Margin = new Padding(0, 10, 0, 0);
				bool ChoiceLabel = false;
				while (aReader.Read())
				{
					if (ChoiceLabel == false)
					{
						flowPanel.Controls.Add(item1);
						ChoiceLabel = true;
					}
					choice.Add(new ChoiceClass("(" + (choice.Count + 1) + ") " + aReader.GetString(1), aReader.GetInt32(7)));
					for (int i = 3; i < 3 + aReader.GetInt32(2); i++)
						choice.Last().AddQuestion(aReader.GetString(i));
					flowPanel.Controls.Add(choice.Last());
				}

				aReader = aCommand_2.ExecuteReader();

				Label item2 = new Label();
				item2.Text = "判断题";
				item2.AutoSize = true;
				item2.Margin = new Padding(0, 10, 0, 0);
				bool DecideLabel = false;
				while (aReader.Read())
				{
					if (DecideLabel == false)
					{
						flowPanel.Controls.Add(item2);
						DecideLabel = true;
					}
					decide.Add(new DecideClass("(" + (decide.Count + 1) + ") " + aReader.GetString(1), aReader.GetBoolean(2)));
					flowPanel.Controls.Add(decide.Last());
				}

				aReader.Close();
				aConnection.Close();
			}
			catch (OleDbException e)
			{
				MessageBox.Show(e.Message);
			}
		}
		*/
		public static void SetFont(Control control)
		{
			string AppPath = System.IO.Directory.GetCurrentDirectory();
			try
			{
				PrivateFontCollection font = new PrivateFontCollection();
				font.AddFontFile(AppPath + @"\Font\MSYHBD.TTF");
				Font myFont = new Font(font.Families[0].Name, 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
				control.Font = myFont;
			}
			catch
			{
				//MessageBox.Show("字体不存在或加载失败\n程序将以默认字体显示", "警告",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}
	}
}
